package com.xuetang9.t9.hotel.controller;

import com.xuetang9.t9.hotel.beans.RoomInfo;
import com.xuetang9.t9.hotel.mapper.condition.impl.RoomInfoCondition;
import com.xuetang9.t9.hotel.services.IRoomInfoService;
import com.xuetang9.t9.hotel.utils.HttpCodeEnum;
import com.xuetang9.t9.hotel.utils.PageBean;
import com.xuetang9.t9.hotel.utils.ResponseResult;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;


/**
 * 房间controller
 */
@Controller
@RequestMapping("/room")
@ResponseBody
public class RoomInfoController {

    @Autowired
    private IRoomInfoService roomInfoService;

    /**
     * 总览房间
     * @param condition
     * @return
     */
    @RequestMapping(value = "/show",method = RequestMethod.POST)
    private PageBean show(@RequestBody RoomInfoCondition condition){
        return roomInfoService.findByAll(condition);
    }

    /**
     * 房间保存
     * @param roomInfo
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    private ResponseResult save(@RequestBody RoomInfo roomInfo){
        if(roomInfo == null){
            return ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
        }
        if(roomInfoService.save(roomInfo)){
            return ResponseResult.okResult(HttpCodeEnum.SUCCESS.getCode(),HttpCodeEnum.SUCCESS.getErrorMessage());
        }
        return ResponseResult.okResult(HttpCodeEnum.SERVER_ERROR.getCode(),HttpCodeEnum.SERVER_ERROR.getErrorMessage());
    }

    /**
     * 房间修改
     * @param roomInfo
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    private ResponseResult update(@RequestBody RoomInfo roomInfo){
        if(roomInfo == null){
            return ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
        }
        if(roomInfoService.update(roomInfo)){
            return ResponseResult.okResult(HttpCodeEnum.SUCCESS.getCode(),HttpCodeEnum.SUCCESS.getErrorMessage());
        }
        return ResponseResult.okResult(HttpCodeEnum.SERVER_ERROR.getCode(),HttpCodeEnum.SERVER_ERROR.getErrorMessage());
    }

    /**
     * 房间删除
     * @param
     * @return
     */
    @RequestMapping(value = "/remove/{id}")
    private ResponseResult save(@PathVariable("id") Integer id){
        if(id == null){
            return ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
        }
        if(roomInfoService.remove(id)){
            return ResponseResult.okResult(HttpCodeEnum.SUCCESS.getCode(),HttpCodeEnum.SUCCESS.getErrorMessage());
        }
        return ResponseResult.okResult(HttpCodeEnum.SERVER_ERROR.getCode(),HttpCodeEnum.SERVER_ERROR.getErrorMessage());
    }

    /**
     * 批量修改房间状态
     * @param
     * @return
     */
    @RequestMapping(value = "/batch/state/{id}")
    private ResponseResult save(@RequestBody List<Integer> rooms,@PathVariable("id") Integer id){
        if(rooms == null || rooms.size() == 0){
            return ResponseResult.okResult(HttpCodeEnum.PARAM_INVALID.getCode(),HttpCodeEnum.PARAM_INVALID.getErrorMessage());
        }
        if(roomInfoService.batchByStart(rooms,id)){
            return ResponseResult.okResult(roomInfoService.findByAll(new RoomInfoCondition()));
        }
        return ResponseResult.okResult(HttpCodeEnum.SERVER_ERROR.getCode(),HttpCodeEnum.SERVER_ERROR.getErrorMessage());
    }
}
